package com.cac.demo.util.adapter;

import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.List;

/**
 * @className: RequestContextHolder
 * @description: （描述这个类的作用）
 * @author:
 * @date: 2024/6/22 11:28:28 周六
 * @version: 1.0
 */
public class RequestContextHolder {

    private static final TransmittableThreadLocal<String> userIdThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<String> userNameThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<String> deptIdThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<String> ipThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<String> requestPathThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<Object[]> requestDataThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<Object> returnDataThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<String> returnDataTypeThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<String> targetMethodThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<List<String>> targetMethodParametersThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<String> tokenThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<String> sessionIdThreadLocal = new TransmittableThreadLocal();
    private static final TransmittableThreadLocal<Boolean> cannotDiscardThreadLocal = new TransmittableThreadLocal();

    public RequestContextHolder() {
    }

    public static void setUserId(String userId) {
        userIdThreadLocal.set(userId);
    }

    public static String getUserId() {
        return (String)userIdThreadLocal.get();
    }

    public static void setDeptId(String deptId) {
        deptIdThreadLocal.set(deptId);
    }

    public static String getDeptId() {
        return (String)deptIdThreadLocal.get();
    }

    public static void setUserName(String userName) {
        userNameThreadLocal.set(userName);
    }

    public static String getUserName() {
        return (String)userNameThreadLocal.get();
    }

    public static void setIp(String ip) {
        ipThreadLocal.set(ip);
    }

    public static String getIp() {
        return (String)ipThreadLocal.get();
    }

    public static void setToken(String token) {
        tokenThreadLocal.set(token);
    }

    public static String getToken() {
        return (String)tokenThreadLocal.get();
    }

    public static void setSessionId(String sessionId) {
        sessionIdThreadLocal.set(sessionId);
    }

    public static String getSessionId() {
        return (String)sessionIdThreadLocal.get();
    }

    public static void setRequestPath(String requestPath) {
        requestPathThreadLocal.set(requestPath);
    }

    public static String getRequestPath() {
        return (String)requestPathThreadLocal.get();
    }

    public static void setTargetMethod(String targetMethod) {
        targetMethodThreadLocal.set(targetMethod);
    }

    public static String getTargetMethod() {
        return (String)targetMethodThreadLocal.get();
    }

    public static void setRequestData(Object[] requestData) {
        requestDataThreadLocal.set(requestData);
    }

    public static Object[] getRequestData() {
        return (Object[])requestDataThreadLocal.get();
    }

    public static Object getReturnData() {
        return returnDataThreadLocal.get();
    }

    public static void setReturnData(Object returnData) {
        returnDataThreadLocal.set(returnData);
    }

    public static String getReturnDataType() {
        return (String)returnDataTypeThreadLocal.get();
    }

    public static void setReturnDataType(String returnDataType) {
        returnDataTypeThreadLocal.set(returnDataType);
    }

    public static void setTargetMethodParameters(List<String> targetMethodParameters) {
        targetMethodParametersThreadLocal.set(targetMethodParameters);
    }

    public static List<String> getTargetMethodParameters() {
        return (List)targetMethodParametersThreadLocal.get();
    }

    public static Boolean getCannotDiscard() {
        Boolean cannotDiscard = (Boolean)cannotDiscardThreadLocal.get();
        return cannotDiscard != null && cannotDiscard;
    }

    public static void setCannotDiscard(boolean canReject) {
        cannotDiscardThreadLocal.set(canReject);
    }

    public static void clear() {
        userIdThreadLocal.remove();
        userIdThreadLocal.remove();
        userNameThreadLocal.remove();
        deptIdThreadLocal.remove();
        ipThreadLocal.remove();
        requestPathThreadLocal.remove();
        requestDataThreadLocal.remove();
        targetMethodThreadLocal.remove();
        targetMethodParametersThreadLocal.remove();
        tokenThreadLocal.remove();
        sessionIdThreadLocal.remove();
        cannotDiscardThreadLocal.set(false);
    }
}
